package www.openteam.com.exception;

import android.net.ParseException;
import android.util.Log;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import static www.openteam.com.exception.ErrorType.NULL_DATA_ERROR;

/**
 * Created by kylin on 2016/12/19.
 */

public class ExceptionEngine {
    //对应HTTP的状态码
    private static final int FAIL = 0;
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    private static final String TAG =ExceptionEngine.class.getSimpleName() ;

    public static ApiException handleException(Throwable throwable) {
        ApiException apiException;
        if (throwable instanceof ServerException) {

            ServerException httpException = (ServerException) throwable;
            apiException = new ApiException(throwable, ErrorType.HTTP_ERROR);
            Log.d(TAG, "handleException: "+httpException.message);
            switch (httpException.code) {
                case FAIL:
                    apiException.message = httpException.message;
                    break;
                case NULL_DATA_ERROR:
                    apiException.message=httpException.message;
                    break;
                case UNAUTHORIZED:
                    apiException.message = "当前请求需要用户验证";
                    break;
                case FORBIDDEN:
                    apiException.message = "服务器已经理解请求，但是拒绝执行它";
                    break;
                case NOT_FOUND:
                    apiException.message = "服务器异常，请稍后再试";
                    break;
                case REQUEST_TIMEOUT:
                    apiException.message = "请求超时";
                    break;
                case GATEWAY_TIMEOUT:
                    apiException.message = "作为网关或者代理工作的服务器尝试执行请求时，未能及时从上游服务器（URI标识出的服务器，例如HTTP、FTP、LDAP" +
                            "）或者辅助服务器（例如DNS）收到响应";
                    break;
                case INTERNAL_SERVER_ERROR:
                    apiException.message = "服务器遇到了一个未曾预料的状况，导致了它无法完成对请求的处理";
                    break;
                case BAD_GATEWAY:
                    apiException.message = "作为网关或者代理工作的服务器尝试执行请求时，从上游服务器接收到无效的响应";
                    break;
                case SERVICE_UNAVAILABLE:
                    apiException.message = "由于临时的服务器维护或者过载，服务器当前无法处理请求";
                    break;
                default:
                    apiException.message = "网络错误";  //其它均视为网络错误
                    break;
            }
            return apiException;
        } else if (throwable instanceof ServerException) {
            ServerException serverException = (ServerException) throwable;
            apiException = new ApiException(serverException, serverException.code);
            apiException.message=serverException.message;
            return apiException;
        }
        else if (throwable instanceof JSONException ||
                throwable instanceof ParseException){
            apiException =new ApiException(throwable,ErrorType.PARSE_ERROR);
            apiException.message ="解析错误";
            return apiException;
        }else if (throwable instanceof ConnectException ||
                throwable instanceof SocketTimeoutException
                || throwable instanceof ConnectTimeoutException){
            if ("HTTP 404 Not Found".equals(throwable.getMessage())){
                apiException =new ApiException(throwable,ErrorType.NETWORK_ERROR);
                apiException.message ="没有连接服务器";
            }else{
                apiException =new ApiException(throwable,ErrorType.NETWORK_ERROR);
                apiException.message = "连接服务器错误";
            }
            return apiException;
        }else{
            apiException =new ApiException(throwable,ErrorType.UNKONW);
            apiException.message ="未知错误";
            return apiException;
        }

    }


}
